<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.w3.org/1999/xhtml">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
    <h1>
        生产报表导入
    </h1>
</section>
<section class="content">
    <form id="form">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">Excel信息导入</h3>
                    </div>
                    <div class="box-body">
                        <div class="form-group">
                            <label>模版</label>
                            <a th:href="@{/file/生产日报导入模板.xls}">点击下载</a>
                        </div>
                        <div class="form-group">
                            <label>上次导入错误数据</label>
                            <a id="download-error"
                               th:data-url="@{/files/{user}/dailyError.xls(user=${session.user.account})}"
                               href="javascript:void(0);">点击下载</a>
                        </div>
                        <div class="form-group">
                            <label for="yearMonth">生产日报上传年月份</label>
                            <input type="text" class="form-control col-md-3" id="yearMonth" name="yearMonth"
                                   th:value="${#dates.format(date, 'yyyy-MM')}" readonly="readonly"/>
                        </div>
                        <div class="form-group">
                            <label for="upload_file">选择Excel文件并上传</label>
                            <input class="btn" type="file" multiple="multiple" id="upload_file" name="file">
                            <p class="help-block">请按模板进行生产日报导入，信息与模板信息对应。</p>
                        </div>
                    </div>
                    <div class="box-footer">
                        <button shiro:hasPermission="board:dailyTotal" id="btn-upload" type="button"
                                class="btn btn-success">
                            <!--<i class="fa fa-upload"></i>-->
                            <span class="Bold">&nbsp;上传</span>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </form>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    $(function () {

        // 下载导入失败excel
        $("#download-error").click(function () {
            var that = $(this);

            js.post({
                url: ctx + "board/dailyTotal/exist",
                data: {
                    url: that.attr("data-url")
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        that.attr("href", that.attr("data-url") + "?timeStamp=" + new Date().getTime());
                    } else {
                        //还没有需要处理的文件
                        js.modal.warning("还没有需要处理的文件");
                    }
                    js.modal.closeLoading();
                }
            });
        });

        /**
         * 验证
         */
        js.validate.init("form", {
            fields: {
                file: {
                    validators: {
                        notEmpty: {message: '上传文件不能为空'},
                        file: {extension: 'xls,xlsx', message: '请选择excel文件'}
                    }
                },
                yearMonth: {validators: {notEmpty: {message: '日报所属年月不能为空'}}}
            }
        });

        /**
         * 上传文件
         */
        $("#btn-upload").click(function () {
            js.validSubmit({
                formId: "form",
                url: ctx + "board/dailyTotal/import",
                data: new FormData($("#form")[0]),
                success: function (result) {
                    debugger;
                    if (result.type === web_status.SUCCESS) {
                        js.modal.alert(result.msg);
                    } else {
                        js.modal.alert("部分数据导入失败，请下载上次导入错误数据。");
                    }
                    js.modal.closeLoading();
                },
                error: function () {
                    js.modal.alert("部分数据导入失败，请下载上次导入错误数据。");
                    js.modal.closeLoading();
                }
            });
        });

        // 日期控件
        $('#yearMonth').datepicker({
            format: 'yyyy-mm',
            language: 'zh-cn',
            autoclose: true,
            clearBtn: false,
            startView: 1,
            minViewMode: 1,
            maxViewMode: 2
        });

    });

</script>
</body>
</html>
